<?php
namespace app\admin\controller;
use controller\BasicAdmin;
use think\Db;
use app\admin\model\userModel;
class Adminuser extends BasicAdmin{

    public $table = 'user';
    /**
     * 用户列表页
     */
    public function indexList(){
        //等级
        $userModel = new userModel();
        $result['role'] = $userModel->userRole();
        //状态
        $result['type'] = ['删除','正常'];
        list($get, $db) = [$this->request->get(), Db::name($this->table)->alias('a')->join('t_user_role b','a.role_id=b.id')->join('t_user_role c','a.role_id=c.id')];
        //搜索
        isset($get['type']) && $get['type'] != '' ? $where = "a.status= {$get['type']}"  : $where = 'a.status >= 0';
        isset($get['role']) && $get['role'] != 0 ? $where .=" AND a.role_id={$get['role']}" : $where .= ' AND a.role_id >= 0';
        isset($get['username']) && $get['username']!='' ? $where .=" AND (a.username like '%{$get['username']}%' OR a.nickname like '%{$get['username']}%' OR a.mobile='{$get['username']}')" :'';
        $list = parent::_list($db->where($where)->order('a.add_time DESC')->field('a.id,a.nickname,a.username,a.mobile,a.add_time,a.pid,a.is_delete,c.name'),1,'','',$result);
        $userModel = new userModel();
        $list['list'] = $userModel->getParentUser($list['list']);
        $list['search'] = $get;
        return $this->fetch('', ['title' => '用户详情','list'=>$list]);

    }

    public function edit(){
        $id = $this->request->request('id');
        $info = Db::name($this->table)
            ->alias('a')
            ->where(['a.id'=>$id])
            ->field('a.id,a.username,a.nickname,a.password,a.pay_password,a.mobile,a.pid,a.referee_id,a.is_delete,a.role_id,a.status,a.agent_id')
            ->find();
        //上级
        $info['paterName']= Db::name($this->table)->where(['id'=>$info['pid']])->value('username');
        //团队人数
        $info['paterCount']= Db::name($this->table)->where(['pid'=>$info['id']])->count();
        //推荐人
        $info['referee_id'] != 0 ? $info['refereeName'] = Db($this->table)->where(['id'=>$info['referee_id']])->value('username'):$info['refereeName']='';
        //二级代理商
        $info['agent_id'] !=0 ? $info['agentName'] = Db($this->table)->where(['id'=>$info['agent_id']])->value('username'):$info['agentName']='';
        //等级
        $userModel = new userModel();
        $role = $userModel->userRole();

        return $this->fetch('', ['title' => '用户详情', 'info' => $info,'role'=>$role]);
    }

    /**
     * 保存修改
     */
    public function postEdit(){
        $id = $this->request->request('id');
        $data['password'] = $this->request->request('password');//密码
        $info = Db::name($this->table)->where(['id'=>$id])->find();
        if(empty($info))$this->error('暂无该用户');
        if($info['password'] != $data['password']){
            $data['password'] = md5(md5($data['password']).$info['salt']);
        }
        $data['username'] = $this->request->request('username');//用户名

        $data['pay_password'] = $this->request->request('pay_password');//支付密码

        $data['mobile'] = $this->request->request('mobile');//手机号码

        $data['pid'] = $this->request->request('pid');//上级id

        $data['agent_id'] = $this->request->request('agent_id');//二级代理商

        $data['referee_id'] = $this->request->request('referee_id');//推荐人

        $data['role_id'] = $this->request->request('role');//用户等级

        $data['status'] = $this->request->request('status');//用户状态

        $result = Db($this->table)->where(['id'=>$id])->update($data);

        if(!empty($result)){
            $this->success('恭喜, 数据保存成功!', '');
        }else{
            $this->error('数据没有变动');
        }

    }

    /**
     * 修改状态
     */
    public function editStatus(){
        list($post, $db) = [$this->request->post(), Db::name($this->table)];
        $post['value'] == 1 ? $data['is_delete'] = 0 : $data['is_delete'] = 1;
        $result = $db->where(['id'=>$post['id']])->update($data);
        if(!empty($result)){
            $this->success('用户状态已修改','');
        }else{
            $this->error('修改失败','');
        }
    }

    /**
     * 红包详情
     */

    public function capiDetails(){
        $id = $this->request->request('id');//id
        if(empty($id))$this->error('数据保存失败');

        $cInfo = Db::name('user')
            ->alias('a')
            ->join('t_capital b','a.id=b.uid','LEFT')
            ->join('t_user_role c','a.role_id=c.id','LEFT')
            ->where(['a.id'=>$id,'b.type'=>2])
            ->field('a.username,a.nickname,a.role_id,b.*,c.name')
            ->find();

        //最大消费额度
        if($cInfo['role_id']==1){
            $cInfo['type'] = 'xf_hbe';
        }else if($cInfo['role_id']==2){
            $cInfo['type'] = 'cz_hbe';
        }else if($cInfo['role_id'] == 3){
            $cInfo['type'] = 'gd_hbe';
        }

        $cInfo['red_max'] = Db::name('config')->where(['name'=>$cInfo['type']])->value('data');
        $title = $cInfo['username'].'用户资金';
        return $this->fetch('', ['title' => "$title", 'info' => $cInfo]);
    }

    /**
     * 团队
     */
    public function userTeam(){
        $id = $this->request->request('id');//id
        if(empty($id))$this->error('错误操作');
        //一级团队
        $info = Db::name($this->table)->where(['id'=>$id])->find();
        $userModel = new userModel();

        $noeTeam = $userModel->getNoeTeam($info['id']);

        //二级团队
        if(!empty($noeTeam)){
            $twoTeam = $userModel->getTwoTeam($info['id']);
        }
        return $this->fetch('', ['title' => '用户管理', 'list' =>$noeTeam]);
    }

    /**
     * 会员红包提现记录
     */
    public function redWithdraw(){
        list($get,$db) = [$this->request->request(),Db::name('withdraw')];
        isset($get['id']) && $get['id'] !='' ? $db->where(['uid'=>$get['id'],'usertype'=>1,'type'=>1]) : '';
        $list = parent::_list($db,1,'','','');
        $username = Db::name('user')->where(['id'=>$get['id']])->value('username');
        return $this->fetch('', ['title' => $username.'红包提现记录', 'list' =>$list]);
    }
    /**
     * 会员充值记录
     */
    public function userRecharge(){
        list($get,$db) = [$this->request->request(),Db::name('recharge')->alias('a')
            ->join('t_user b','a.uid=b.id','LEFT')->join('shop c','c.id=a.shop_id','LEFT')
            ->field('a.uid,a.money,a.recharge_type,a.addtime,a.status,a.recharge_num,a.shop_id,b.username,c.name')
        ];
        isset($get['uid']) && $get['uid'] !='' ? $db->where(['uid'=>$get['uid']]) : '';
        $list = parent::_list($db,1,'','','');
        $recharge_type = [1=>'支付宝',2=>'微信',3=>'银行卡'];
        $status = [1=>'失败',2=>'成功'];

        foreach ($list['list'] as $key=>$value){
            $list['list'][$key]['recharge_type_name'] = $recharge_type[$value['recharge_type']];
            $list['list'][$key]['status_name'] = $status[$value['status']];
            $list['list'][$key]['addtimes'] = date('Y-m-d H:i:s',$value['addtime']);
        }
        return $this->fetch('', ['title' => '充值记录', 'list' =>$list]);
    }

}